package com.example.transit.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import android.widget.ZoomControls;
import com.example.transit.R;
import com.example.transit.base.BaseActivity;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import butterknife.BindView;
import io.reactivex.Observable;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;


public class StartActivity extends BaseActivity{
    @BindView(R.id.startact_img)
    ImageView startactImg;
    private static final int RC_LOCATION = 100;
    private int sysSdkVersion = Build.VERSION.SDK_INT;
    String[] perms = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.READ_PHONE_STATE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_start;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

        if (sysSdkVersion < Build.VERSION_CODES.M) {
//            handler.sendEmptyMessageDelayed(1, 1000);
            startMain();
        } else {
            RequestAllPermission(); //Android6.0申请权限
        }
    }

    @AfterPermissionGranted(RC_LOCATION)
    private void RequestAllPermission() {
//        Toast.makeText(this, "RequestLocationAndCallPermission,out", Toast.LENGTH_SHORT).show();

        if (EasyPermissions.hasPermissions(this, perms)) {
            //用户同意了权限
            //进行后面的操作
            startMain();

        } else {
            //用户拒绝了权限
            //正常情况（没有勾选《拒绝后不再询问》）会先弹下面这个框一，点击确定会出现系统的框二
            //非正常情况（有勾选《拒绝后不再询问》）框一和系统的框二都不会出现
            //无论哪种情况，最终，都会根据用户的选择同意还是拒绝，而回调对应的方法；（勾选过《拒绝后不再询问》，这种情况也是属于拒绝，所以他也会走拒绝的回调）
            EasyPermissions.requestPermissions(this, "This app needs access to your location and contacts to know where and who you are.",RC_LOCATION, perms);
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case RC_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    handler.sendEmptyMessageDelayed(1, 1000);
                    startMain();
                } else {
                }
                return;
            }
            default:
                break;
        }
    }

    private void startMain() {
        Observable.timer(2, TimeUnit.SECONDS).subscribe(new io.reactivex.functions.Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                overridePendingTransition(R.anim.zoomin,R.anim.zoomout );
                startActivity(intent);
                finish();
            }
        });
    }
    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }




}
